package com.ld.poetry.controller;

import com.ld.poetry.aop.LoginCheck;
import com.ld.poetry.config.PoetryResult;
import com.ld.poetry.aop.SaveCheck;
import com.ld.poetry.utils.storage.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 七牛云存储服务控制器
 * 提供七牛云存储相关的API接口
 * 仅在QiniuUtil bean存在时才会被加载
 */
@RestController
@RequestMapping("/qiniu")
@ConditionalOnBean(QiniuUtil.class)
public class QiniuController {

    // 注入七牛云工具类
    @Autowired
    private QiniuUtil qiniuUtil;

    /**
     * 获取七牛云上传凭证
     * 该凭证用于覆盖指定key的文件
     * 
     * @param key 要覆盖的文件在七牛云存储中的key
     * @return PoetryResult<String> 包含上传凭证的响应结果
     * @apiNote 需要用户登录且通过保存检查
     */
    @GetMapping("/getUpToken")
    @LoginCheck
    @SaveCheck
    public PoetryResult<String> getUpToken(@RequestParam(value = "key") String key) {
        // 调用七牛云工具类获取上传凭证
        return PoetryResult.success(qiniuUtil.getToken(key));
    }
}
